package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.service.CompanyMPSService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;

public class CompanyMPSAction extends ActionSupport {


	private static final long serialVersionUID = 1L;

	private CompanyMPSService companyMPSService;
	private int initPage=1;
	private int pageRow=10;
	private int totalCount;
	private String s_date;
	private String e_date;
	private String order;
	private String sort;
	private String comId;
	private Map<String, String> dateMap;
	private String menuName;
	public CompanyMPSService getCompanyMPSService() {
		return companyMPSService;
	}
	public void setCompanyMPSService(CompanyMPSService companyMPSService) {
		this.companyMPSService = companyMPSService;
	}
	public int getInitPage() {
		return initPage;
	}
	public void setInitPage(int initPage) {
		this.initPage = initPage;
	}
	public int getPageRow() {
		return pageRow;
	}
	public void setPageRow(int pageRow) {
		this.pageRow = pageRow;
	}
	public int getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}
	public String getS_date() {
		return s_date;
	}
	public void setS_date(String s_date) {
		this.s_date = s_date;
	}
	public String getE_date() {
		return e_date;
	}
	public void setE_date(String e_date) {
		this.e_date = e_date;
	}
	public String getOrder() {
		return order;
	}
	public void setOrder(String order) {
		this.order = order;
	}
	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public String getComId() {
		return comId;
	}
	public void setComId(String comId) {
		this.comId = comId;
	}
	public Map<String, String> getDateMap() {
		return dateMap;
	}
	public void setDateMap(Map<String, String> dateMap) {
		this.dateMap = dateMap;
	}
	
	
	public String getMenuName() {
		return menuName;
	}
	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}
	public String comMPSList(){
		dateMap=DateUtils.getDate();
		return "comMPSList";
	}
	
	/**
	 * 菜单收益
	 */
	public void menuProfitReport(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<Map<String, Object>> mpss=this.getCompanyMPSService().getMPS(menuName,comId, s_date, e_date);
		List<String> dateList=new ArrayList<String>();
		List<Double> menuProfit=new ArrayList<Double>();
		List<String> menuName=new ArrayList<String>();
		for (Map<String, Object> mps : mpss) {
			menuProfit.add((Double)mps.get("profit"));
			//System.out.println((Double)mps.get("profit"));
			menuName.add((String)mps.get("menuName"));
			dateList.add((String)mps.get("sDate"));
		}
		JSONArray jodate=JSONArray.fromObject(dateList);
		JSONArray joprofit=JSONArray.fromObject(menuProfit);
		JSONArray joname=JSONArray.fromObject(menuName);
		JSONObject jo=new JSONObject();
		jo.put("profit", joprofit);
		jo.put("menuName", joname);
		jo.put("date", jodate);
		System.out.println(jo);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 菜单数量报表
	 */
	public void menuNumReport(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<Map<String, Object>> mpss=this.getCompanyMPSService().getMPS(menuName,comId, s_date, e_date);
		List<String> dateList=new ArrayList<String>();
		List<Integer> menunum=new ArrayList<Integer>();
		List<String> menuName=new ArrayList<String>();
		for (Map<String, Object> mps : mpss) {
			menunum.add((Integer)mps.get("totalNum"));
			menuName.add((String)mps.get("menuName"));
			dateList.add((String)mps.get("sDate"));
		}
		JSONArray jodate=JSONArray.fromObject(dateList);
		JSONArray jonum=JSONArray.fromObject(menunum);
		JSONArray joname=JSONArray.fromObject(menuName);
		JSONObject jo=new JSONObject();
		jo.put("totalnum", jonum);
		jo.put("menuName", joname);
		jo.put("date", jodate);
		System.out.println(jo);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	
	public void getComMPS(){
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		int offset=(initPage-1)*pageRow;
		if(s_date==null||s_date.equals("")){
			s_date=DateUtils.get30Days();
		}
		if(e_date==null||e_date.equals("")){
			e_date=DateUtils.getCurrentDate();
		}
		List<Map<String, Object>> listmps=this.getCompanyMPSService().getComMPS(offset, pageRow,menuName, comId, s_date, e_date, order, sort);
		totalCount=this.getCompanyMPSService().mpsCount(menuName,comId, s_date, e_date);
		JSONArray jomps=JSONArray.fromObject(listmps);
		JSONObject jo=JsonUtils.buildJson(jomps, initPage, pageRow, totalCount);
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(jo);
		out.print(jo);
		out.flush();
		out.close();
	}
}
